package com.qiangesoft.qrcode.utils;

import com.google.zxing.WriterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;

@RestController
public class QRCodeController {

    @Autowired
    private QRCodeService qrCodeService;

    @GetMapping("/generateQRCode")
    public void generateQRCode(@RequestParam String text, HttpServletResponse response) {
        try {
            byte[] qrCodeImageBytes = qrCodeService.generateQRCodeWithLogo(text);

            // 设置响应头
            response.setContentType("image/png");
            response.addHeader("Content-Disposition", "attachment; filename=qr_code_with_logo.png");
            response.getOutputStream().write(qrCodeImageBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}